package com.yu.crm.utils;

import com.yu.crm.domain.response.CondectionTypes;
import com.yu.crm.domain.response.ConditionInfoRep;
import com.yu.crm.domain.response.DetailedConditions;
import com.yxzz.common.helper.DataHelpers;

import java.util.List;
import java.util.Map;

public class ConditionUtil {

    /**
     * 单一条件比较
     * @return
     */
    public static boolean ConditionMap(Map<String, Integer> map, ConditionInfoRep conditionInfoDetail) {
        if (DataHelpers.isEmpty(conditionInfoDetail)) {
            return true;
        }
        List<DetailedConditions> detailedConditionsList = conditionInfoDetail.getDetailedConditionsList();
        if (DataHelpers.isEmpty(detailedConditionsList) || detailedConditionsList.size()==0) {
            return true;
        }
        if (DataHelpers.isEmpty(map)) {
            return false;
        }
        Integer i = 0;

        for (DetailedConditions conditions:detailedConditionsList
             ) {
            Integer conditionValue = Integer.parseInt(conditions.getConditionValue());

            if (map.containsKey(conditions.getConName())) {
                //变量名
                //获取变量值
                Integer value = map.get(conditions.getConName());
                //判断数据库保存的判断条件
                // =
                if (conditions.getConditionSymbol().equals(CondectionTypes.EQ.getKey())) {
                    if (value.equals(conditionValue)) {
                        i += 1;
                    }
                }
                // !=
                if (conditions.getConditionSymbol().equals(CondectionTypes.NO_EQ.getKey())) {
                   if (!value.equals(conditionValue)){
                        i += 1;
                    }
                }
                // >
                if (conditions.getConditionSymbol().equals(CondectionTypes.GT.getKey())) {
                    if (value > conditionValue) {
                        i += 1;
                    }
                }
                // <
                if (conditions.getConditionSymbol().equals(CondectionTypes.LT.getKey())) {
                    if ( value < conditionValue) {
                        i += 1;
                    }
                }
                // >=
                if (conditions.getConditionSymbol().equals(CondectionTypes.GE.getKey())) {
                    if (value >= conditionValue) {
                        i += 1;
                    }
                }
                //<=
                if (conditions.getConditionSymbol().equals(CondectionTypes.LE.getKey())) {
                    if (value <= conditionValue) {
                        i += 1;
                    }
                }
                //包含
                if (conditions.getConditionSymbol().equals(CondectionTypes.CONTAIN.getKey())) {
                    if (value.toString().contains(conditionValue.toString())) {
                        i += 1;
                    }
                }
            }
        }
        //与
       /* if (conditionInfoDetail.getCombiType().equals("0")) {

        }*/
        if (conditionInfoDetail.getCombiType().equals("1")) {
            //或
            if (i>0) {
                return true;
            }else {
                return false;
            }
        }else {
            if (i == detailedConditionsList.size()) {
                return true;
            }else {
                return false;
            }
        }
    }
}
